package com.dp.Observer.principle.server.impl;

import com.dp.Observer.principle.server.Observer;
import com.dp.Observer.principle.server.Subject;

import java.util.ArrayList;
import java.util.List;

public class ObjectFor3D implements Subject {
    private List<Observer> observers = new ArrayList<Observer>();
    /**
     * 3D彩票的号码
     */
    private String msg;

    /**
     * 方法说明: 注册一个观察者
     * @param observer
     * @return void
     * @author liangalong[liangalong2022@aliyun.com]
     * @date 2023-05-05 16:32*/
    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    /**
     * 方法说明: 移除一个观察者
     * @param observer
     * @return void
     * @author liangalong[liangalong2022@aliyun.com]
     * @date 2023-05-05 16:32*/
    @Override
    public void removeObserver(Observer observer) {
        int index = observers.indexOf(observer);
        if (index >= 0) {
            observers.remove(index);
        }
    }

    /**
     * 方法说明: 通知所有观察者
     * @param
     * @return void
     * @author liangalong[liangalong2022@aliyun.com]
     * @date 2023-05-05 16:33*/
    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(msg);
        }
    }

    /**
     * 主题更新消息
     *
     * @param msg
     */
    public void setMsg(String msg) {
        this.msg = msg;
        notifyObservers();
    }


}
